package com.chuangke.metadata.controller;

import com.alibaba.druid.util.StringUtils;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.metadata.entity.SysBisModel;
import com.chuangke.metadata.entity.SysBisModelDetail;
import com.chuangke.metadata.service.SysBisModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chuangke
 * @date 2024-01-04
 */
@RestController
@RequestMapping("bis/model")
public class SysBisModelController {

	@Autowired
	private SysBisModelService sysBisModelService;


	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/page")
	public HttpResult page(@RequestBody Map<String, Object> pageQuery) {
		return HttpResult.ok(sysBisModelService.page(new CkPageQuery(pageQuery)));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody SysBisModel record) {
		if(StringUtils.isEmpty(record.getId())) {
			record.setId(IdManager.nextId());
		}
		sysBisModelService.saveOrUpdate(record);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<String> ids) {
		sysBisModelService.delete(ids);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/{id}")
	public HttpResult load(@PathVariable("id") String id) {
		return HttpResult.ok(sysBisModelService.load(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/saveDetail")
	public HttpResult saveDetail(@RequestBody SysBisModelDetail detail) {
		sysBisModelService.saveDetail(detail);
		return HttpResult.ok() ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/delDetailByIds")
	public HttpResult delDetailByIds(@RequestBody List<String> ids) {
		sysBisModelService.deleteDetail(ids);
		return HttpResult.ok();
	}
}
